package com.example.websocketdemo.udpUtil;

import com.google.common.util.concurrent.RateLimiter;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

/**
 * TODO
 *
 * @ClassName UdpClient
 * @Author xiaojiang
 * @Date 2021/11/12 下午 14:41
 */
@Slf4j
public class UdpClient {

    // 发送
    public static void main(String[] args) throws Exception{
//        String data="imsi-46012000000006d00d2004700170003696d73066d6e63303031066d636334363004677072737f0001000080000100004a001001001000170000c6190000000000000000490001000657001500471b2694ea200104683000000000000000000006598800060005636d6e65745d00510049000100065700150041001e8481200204683761000000000000004500345700150145048881ea20010468300000000000000000001071500016004509000000000000000000000000000000000000000048000800000f4240000f42404d000700000000000040006300010002";
        long l = System.currentTimeMillis();
        log.info("开始时间为：" + l);
        Long insi = 460120000000001L;
        /*for (int i = 0; i < 2000; i++) {
         */
        /* String ueEpsPdnConnection = "bQDRAEcAGQAFY21uZXQGbW5jMDAzBm1jYzQ2MARncHJzSAAIAACcQAAAnEAASQABAAVXABUARyZyWvAgAQRoUAAAEABQAhEAAAAzXQBFAEkAAQAFUAAWAEwFAAAAAAAAAAAAAAAAAAAAAAAAAABXABUBRVKhMJMgAQRoUAAAEABQAhgAAAABVwAJAIEAAAAAAAAAAGMAAQACSgAQASABBGhQcAAAFFjaNsUrM9iIACQABXNtZjAyBm1uYzAwOAZtY2M0NjALM2dwcG5ldHdvcmsDb3Jn";
            byte[] bytes = RSATest.decodeBase64(ueEpsPdnConnection);
            String s = ByteUtil.toHexString1(bytes);
            String imsi = ByteUtil.toHexString1(insi.getBytes());
            String s1 = "85" + imsi + "06" + s;
            System.out.println("s1="+s1);
            byte[] by = ByteUtil.toBytes(s1);
            String reqBody = new String(by);
            sendPDU(by);*/
        /*
            sendImsi(insi);
            insi++;
        }*/

        long lll = System.currentTimeMillis();
        System.out.println(lll-l);
        RateLimiter rateLimiter = RateLimiter.create(700);
        boolean b = false;
        while ((lll-l)<10000){
            b = false;

            while (b == false) {
                b = rateLimiter.tryAcquire(1);
            }
            sendImsi(insi);
            insi++;
            lll = System.currentTimeMillis();
        }
        long ll = System.currentTimeMillis();
        log.info("结束时间为：" + ll);
        log.info("总用时为：" + (ll-l));

    }

    public static void sendPDU(byte[] data) {
        DatagramSocket datagramSocket=null;
        try {
            //实例化套接字，并指定发送端口
            datagramSocket = new DatagramSocket(30018);
            //指定数据目的地的地址，以及目标端口
            InetAddress destination = InetAddress.getByName("2408:8420:6e50:cb9b:9926:b584:466:3c22");
            DatagramPacket datagramPacket =
//                    new DatagramPacket(data.getBytes(), data.getBytes().length, destination, 30018);
                    new DatagramPacket(data, data.length, destination, 30018);

            //发送数据
            datagramSocket.send(datagramPacket);
        } catch (SocketException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            datagramSocket.close();
        }
    }


    // 发送 ImsiUdp 消息
    public static void sendImsi(Long insi) {
        String imsi = String.valueOf(insi);
        System.out.println("准备发送 ImsiUdp 消息=" + imsi);
        DatagramSocket datagramSocket = null;
        try {
            // 实例化套接字，并指定发送端口
            datagramSocket = new DatagramSocket();
            // 指定数据目的地的地址，以及目标端口
            InetAddress destination = InetAddress.getByName("192.168.3.36");
            DatagramPacket datagramPacket = new DatagramPacket(imsi.getBytes(), imsi.getBytes().length, destination, 31318);
            // 发送数据
            datagramSocket.send(datagramPacket);
            System.out.println("发送 ImsiUdp 消息已发送=" + imsi);
        } catch (SocketException e) {
            System.out.println("ImsiUdp 消息发送失败=" + imsi);
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("ImsiUdp 消息发送失败-" + imsi);
            e.printStackTrace();
        } finally {
            datagramSocket.close();
        }
    }




}
